//
//  TravelNoteViewCell.m
//  ProductB
//
//  Created by 顾佳辉 on 16/5/28.
//  Copyright © 2016年 ChenZheng. All rights reserved.
//

#import "TravelNoteViewCell.h"
#import "STPhotoBroswer.h"
@implementation TravelNoteViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ( self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
    }
    return  self;
}

//点赞
- (IBAction)like:(id)sender {
}

//评论
- (IBAction)comment:(id)sender {
}

//收藏
- (IBAction)favourite:(id)sender {
}

//更多
- (IBAction)more:(id)sender {
}

//出处
- (IBAction)formTitle:(id)sender {
}

- (void)setModel:(trabelnoteModel *)model
{
    CGFloat width = self.contentscrollView.frame.size.width/2.5;
    CGFloat height = self.contentscrollView.frame.size.height;
    _model = model;
    self.title.text = model.topic;
    [self.form setTitle:model.fromname forState:UIControlStateNormal];
    NSDictionary *imagedic = model.contentArray.firstObject;
    self.contentscrollView.contentSize =  CGSizeMake((model.contentArray.count - 1)* width, height);
    self.contentscrollView.showsVerticalScrollIndicator = NO;
    self.contentscrollView.showsHorizontalScrollIndicator = NO;
    //添加滚动图片
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 1; i<model.contentArray.count; i++) {
        [array addObject:model.contentArray[i][@"photo_url"]];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(width *(i - 1)+ (i - 1 ) * 3, 3, width, height - 3)];
        imageView.tag = 101 + i;
        imageView.userInteractionEnabled = YES;
        [imageView sd_setImageWithURL:[NSURL URLWithString:array[i - 1]]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        
        [imageView addGestureRecognizer:tap];
        [self.contentscrollView addSubview:imageView];
    }
    //获取全部图片
    self.allimageArray = [NSMutableArray array];
    self.photoname = [NSMutableArray array];
    for (int i = 0; i<model.contentArray.count; i++) {
        [self.allimageArray addObject:model.contentArray[i][@"photo_url"]];
        [self.photoname addObject:model.contentArray[i][@"caption"]];
    }
    [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:imagedic[@"photo_url"]]];
    self.contentImageView.tag = 101;
    self.contentImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.contentImageView addGestureRecognizer:tap];
    
    
    [self.iconimage  sd_setImageWithURL:[NSURL URLWithString:model.photo]];
     NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    
    CGRect bounds = [model.newdescription boundingRectWithSize:CGSizeMake(320, 1000) options:NSStringDrawingUsesLineFragmentOrigin  attributes:dic context:nil];
    CGRect frame = self.content.frame;
    frame.size = CGSizeMake(320, bounds.size.height);
    self.content.frame = frame;
    self.content.text = model.newdescription;
   
    
}

- (void)tapAction:(UITapGestureRecognizer *)sender{
   UITapGestureRecognizer *tap = (UITapGestureRecognizer*)sender;;
    STPhotoBroswer * broser = [[STPhotoBroswer alloc]initWithImageArray:self.allimageArray currentIndex:[tap view].tag - 101 photoName:self.photoname];
    [broser show];
}

@end
